package com.crazymo.bitmapbase64;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class MainActivity extends AppCompatActivity {

    ImageView imvSrc, imvTarget;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }


    @Override
    protected void onResume() {
        super.onResume();
        imvSrc.setImageBitmap(getLoacalBitmap(Environment.getExternalStorageDirectory().getPath() + "/aibord-release.JPG"));
    }


    /**
     * 加载本地图片
     *
     * @param url
     * @return
     */
    public static Bitmap getLoacalBitmap(String url) {
        try {
            FileInputStream fis = new FileInputStream(url);
            return BitmapFactory.decodeStream(fis);  ///把流转化为Bitmap图片

        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return null;
        }
    }

    private void init() {
        imvSrc = findViewById(R.id.imv_src);
        imvTarget = findViewById(R.id.imv_target);
    }

    public void base64Img(View view) {
        if (view.getId() == R.id.btn_to_base64) {
            String ret = BitmapUtil.bitmap2Base64(Environment.getExternalStorageDirectory().getPath() + "/aibord-release.JPG");
            FileOutputStream outputStream = null;
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
            String dateStr = df.format(new Date());
            String photoName = "qr_aibord-release22";
            imvTarget.setImageBitmap(BitmapUtil.base64ToBitmap(ret));
        } else if (view.getId() == R.id.btn_to_img) {
            ///imvSrc.setImageBitmap(getLoacalBitmap(Environment.getExternalStorageDirectory().getPath() + "/qr_aibord-release22.JPG"));
            String ret = BitmapUtil.bitmap2Base64(Environment.getExternalStorageDirectory().getPath() + "/aibord-release.JPG");
            BitmapUtil.base64ToBitmap(ret,BitmapUtil.getSDPath()+"/dd/","demo2",BitmapUtil.ImageType.JPG);
        }
    }
}
